<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/">活动管理</a></el-breadcrumb-item>
      <el-breadcrumb-item>活动列表</el-breadcrumb-item>
      <el-breadcrumb-item>活动详情</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 内容主体区域 -->
    <el-card class="box-card">
      <!-- 添加分类按钮 -->
      <el-button type="primary" @click="addCates">添加分类</el-button>
      <!-- 表格 -->
      <table-tree
        :data="catesLists"
        :columns="columns"
        :selection-type="false"
        border
        index-text="#"
        :show-index="true"
        :expand-type="false"
      >
        <template slot="isok" slot-scope="scope">
          <i class="el-icon-success" v-if="scope.row.cat_deleted == false" style="color: lightgreen"></i>
          <i class="el-icon-error" v-else style="color: red"></i>
        </template>
        <template slot="grade" slot-scope="scope">
          <el-tag v-if="scope.row.cat_level == 0">一级</el-tag>
          <el-tag type="info" v-else-if="scope.row.cat_level == 1">二级</el-tag>
          <el-tag type="warning" v-else>三级</el-tag>
        </template>
        <template slot="manu">
          <el-button type="primary" size="mini" icon="el-icon-edit">编辑</el-button>
          <el-button type="danger" size="mini" icon="el-icon-delete">删除</el-button>
        </template>
      </table-tree>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="query.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="query.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 添加分类面板 -->
    <el-dialog title="添加分类" :visible.sync="addCatesVisible" width="30%" @close="addCatesClosed">
      <el-form
        :model="cateForm"
        :rules="addCatesrules"
        ref="addCatesruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="cateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类">
          <el-cascader
            v-model="value"
            :props="cascaderProps"
            :options="parentCates"
            @change="handleChange"
          ></el-cascader>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="addCatesVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmAdd">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      catesLists: [],
      addCatesVisible: false,
      cascaderValue: '',
      parentCates: [],
      value: [],
      query: {
        type: '',
        pagenum: 1,
        pagesize: 5,
      },
      total: 0,
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name',
        },
        {
          label: '是否有效',
          type: 'template',
          template: 'isok',
        },
        {
          label: '排序',
          type: 'template',
          template: 'grade',
        },
        {
          label: '操作',
          type: 'template',
          template: 'manu',
        },
      ],
      cascaderProps: {
        label: 'cat_name',
        value: 'cat_id',
        children: 'children',
        checkStrictly: 'true',
        expandTrigger: 'hover',
      },
      cateForm: {
        cat_pid: 0,
        cat_name: '',
        cat_level: 0,
      },
      addCatesrules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { min: 2, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' },
        ],
      },
    }
  },
  created() {
    this.getcatelist()
  },
  methods: {
    async getcatelist() {
      const { data: res } = await this.$http.get('categories', {
        params: this.query,
      })
      if (res.meta.status !== 200) return this.$message.error('获取列表失败')
      this.$message.success('获取列表数据成功')
      console.log(res)
      this.catesLists = res.data.result
      this.total = res.data.total
    },
    handleSizeChange(pagesize) {
      this.query.pagesize = pagesize
      this.getcatelist()
    },
    handleCurrentChange(pagenum) {
      this.query.pagenum = pagenum
      this.getcatelist()
    },
    async addCates() {
      const { data: res } = await this.$http.get('categories', {
        params: {
          type: 2,
        },
      })
      if (res.meta.status !== 200) return this.$message.error('获取父级分类失败')
      this.$message.success('获取父级分类成功')
      console.log()
      this.parentCates = res.data
      this.addCatesVisible = true
    },
    addCatesClosed() {
      this.addCatesVisible = false
      this.value = []
      this.$refs.addCatesruleForm.resetFields()
      this.cateForm.cat_pid = 0
      this.cateForm.cat_level = 0
    },
    handleChange() {
      console.log(this.value)
      if (this.value.length == 0) {
        this.cateForm.cat_pid = 0
        this.cateForm.cat_level = 0
      } else if (this.value.length == 1) {
        this.cateForm.cat_pid = this.value
        this.cateForm.cat_level = 1
      } else if(this.value.length == 2){
        this.cateForm.cat_pid = this.value
        this.cateForm.cat_level = 2
      }
    },
    confirmAdd() {
      this.$refs.addCatesruleForm.validate(async (val) => {
        if (!val) return this.$message.error('请输入信息')
        const { data: res } = await this.$http.post('categories', this.cateForm)
        console.log(res, this.cateForm);
        if (res.meta.status !== 201) return this.$message.error('添加分类失败')
        this.$message.success('添加分类成功')

        //如果是最后一页的话,而且条数已经满了,再加的话就要新增一页
        if (this.query.pagenum == Math.ceil(this.total / this.query.pagesize)) {
          //刷新整个页面
          this.getcatelist()
          // this.total + 1
        } else {
          //如果不是最后一页, 并且添加的是一级菜单的话,应该是添加到最后一页,
          //所以不需要刷新当前页,只需要把总的页数加一
          if(this.cateForm.cat_level == 0) this.total + 1
          else {
            //如果是添加的是二级或者三级菜单的话,就必须要刷新整个页面
            //要不然就看不到新添加的分类
            this.getcatelist()
          }
        }
        this.addCatesVisible = false
      })
      console.log(this.value)
    },
  },
}
</script>

<style scoped lang="less">
.el-button {
  margin-bottom: 10px;
}
.el-cascader {
  width: 100%;
}
</style>
